import { pipeline } from 'stream'
import { promisify } from 'util'
import fetch from 'node-fetch'
import fs from 'node:fs'
import path from 'node:path'
import common from '../../../../lib/common/common.js';
import _ from 'lodash';
const token = '5201314';
async function checkBot(e){
    if (e.adapter_name == 'QQBot')
    return false 
    else return true
}

async function mfm (e, msg = [], dec = '', msgsscr = true) {
  
  if (!Array.isArray(msg)) {msg = [msg]}
  let name = msgsscr ? e.sender.card || e.user_id : e.bot.nickname
  let id = msgsscr ? e.user_id : e.self_id
  if (e.isGroup) {
    try {
      let info = await e.bot.getGroupMemberInfo(e.group_id, id)
      name = info.card || info.nickname
    } catch (err) { }
  }
  let userInfo = {
    user_id: id,
    nickname: name
  }
  let forwardMsg = []
  for (const message of msg) {
    if (!message) { continue }
    forwardMsg.push({...userInfo,message: message})
  }
  /** 制作转发内容 */
  try {
    if (e?.group?.makeForwardMsg) {
      forwardMsg = await e.group.makeForwardMsg(forwardMsg)
    } else if (e?.friend?.makeForwardMsg) {
      forwardMsg = await e.friend.makeForwardMsg(forwardMsg)
    } else {
      return msg.join('\n')
    }
    if (dec) {
      /** 处理描述 */
      if (typeof (forwardMsg.data) === 'object') {
        let detail = forwardMsg.data?.meta?.detail
        if (detail) {
          detail.news = [{ text: dec }]
        }
      } else {
        forwardMsg.data = forwardMsg.data
                .replace('<?xml version="1.0" encoding="utf-8"?>', '<?xml version="1.0" encoding="utf-8" ?>')
          .replace(/\n/g, '')
          .replace(/<title color="#777777" size="26">(.+?)<\/title>/g, '___')
          .replace(/___+/, `<title color="#777777" size="26">${dec}</title>`)
      }
    }
  } catch (err) { }
  return forwardMsg
}
async function check(userQQ) {
    const apiUrl = 'https://api.admilk.top/api.php';
    const response = await fetch(apiUrl);
    const data = await response.json();
    const userQQString = userQQ.toString();
    for (const qq of data.msqq) {
        if (qq === userQQString) {
            return true;
        }
    }
    return false;
}


function mkdirs (dirname) {
  if (fs.existsSync(dirname)) {
    return true
  } else {
    if (mkdirs(path.dirname(dirname))) {
      fs.mkdirSync(dirname)
      return true
    }
  }
}
const CM = { 
    check, 
    mfm, 
    checkBot 
    }
export default CM
//, changetime, randomset, jiejin